<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\City;
use App\Http\Model\Store;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class StoreController extends CommonController
{
    public function index()
    {
        $data = Store::get();
        foreach ($data as $k=>$v){

            $data[$k]['cityname'] =DB::table('city')->where('ctid',$v['zc_city_ctid'])->value('cityname');
        }

        return view('admin.store.index')->with('data',$data);
    }
    //get. admin/store/create  添加分类
    public function create()
    {
        $data = City::get();
//        dd($data);
        return view('admin.store.add')->with('data',$data);
    }
    //post. admin/store  添加分类提交
    public function store()
    {
        $input = Input::except('_token');

        $rules = [
            'mname' => 'required' ,
            'm_address' => 'required' ,

        ];
        $messages = [
            'mname.required'=>'门店名不能为空',
            'm_address.required'=>'门店地址不能为空',
        ];
        //验证数据，验证规则，验证错误返回信息
        $validator = Validator::make($input,$rules,$messages);
        
        if($validator->passes()){
               $re = Store::create($input);
            if($re){
                return redirect('admin/store');
            }else{
                return back()->with('errors','添加失败，请稍后重试');
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    //GET. admin/store/{store}/edit
    public function edit($menid)
    {
        $citydata = City::get();
        $mendata = Store::find($menid);
//        dd($mendata);
        return view('admin.store.edit')->with('citydata',$citydata)->with('mendata',$mendata);
    }
    //PUT. admin/store/{store}
    public function update($menid)
    {
        //$input = Input::all();
        $input = Input::except('_token','_method');
//        dd($input);
        $rules = [
            'mname' => 'required' ,
            'm_address' => 'required' ,
        ];
        $messages = [
            'mname.required'=>'门店名不能为空',
            'm_address.required'=>'门店地址不能为空',
        ];

        $validate = Validator::make($input,$rules,$messages);

        if($validate->passes()){
//            dd($menid);
            $re = Store::where('menid',$menid)->update($input);

            if($re){
                return redirect('admin/store');
            }else{
                return back()->with('errors','修改失败，请稍后再试');
            }

        }else{
            return back()->withErrors($validate);
        }

    }
    // DELETE    admin/store/{store}

    public function destroy($menid)
    {
        
        $re = Store::where('menid',$menid)->delete();

        if($re){
            $data =[
                'status'=> 0 ,
                'msg' => '删除成功！',
            ];
        }else{
            $data =[
                'status'=> 1 ,
                'msg' => '删除失败，请稍后重试！',
            ];
        }
        return $data;
    }

}
